function limitChars(textid, limit, infodiv){
    var text = $('#'+textid).attr("value"); 
    var textlength = text.length;
	
    if(textlength > limit){
    	$('#' + infodiv).html('Вам нельзя написать более чем '+limit+' символов!');
		$('#'+textid).attr("value", text.substr(0,limit));
		return false;
    } else {
   		$('#' + infodiv).html('У Вас осталось '+ (limit - textlength) +' символов.');
		return true;
    }
}

/* Message Alert */
var notify = function (message, options) {
	var block = jQuery('#notifications');
	if (block.length > 0) {
		var settings = jQuery.extend({}, notify.defaults, options);
		var closeButton = settings.closeButton ? '<span class="close-bt"></span>' : '';
		var element = jQuery('#notifications').append('<li>' + message + closeButton + '</li>').children(':last-child');
		element.expand();
		if (settings.autoClose) {
			var timeoutId = setTimeout(function () {
				element.fadeAndRemove();
			}, settings.closeDelay);
			element.hover(function () {
				clearTimeout(timeoutId);
			}, function () {
				timeoutId = setTimeout(function () {
					element.fadeAndRemove();
				}, settings.closeDelay);
			});
		}
	} else {
		setTimeout(function () {
			notify(message, options);
		}, 40);
	}
};
notify.defaults = {
	closeButton: true,
	autoClose: true,
	closeDelay: 8000
};

/* Включение чекбоксов */
var powerChecked = function(options){
	var settings = jQuery.extend({}, powerChecked.defaults, options);	
	var i = 0;
	var delhtmlhidden = '';
	var name_counter  = '';
	switch(settings.typeClick) {
		case "all":
			$("input[name='"+settings.nameInput+"']").each(function(){				
				if(!$(this).is(':checked')) {
					$(this).attr('checked', true);
					i++;
					delhtmlhidden += '<input type="hidden" name="id_delete[]" value="'+$(this).attr('value')+'">';
				} else {
					$(this).attr('checked', false);
				}			
			});
		break;
		
		case "once":
		default:
			$("input[name='"+settings.nameInput+"']:checked").each(function(){
				i++;
				delhtmlhidden += '<input type="hidden" name="id_delete[]" value="'+$(this).attr('value')+'">';
			});
		break;
	}
	if(i)
	{			
		if(i > 4) {
			name_counter = "записей";
		} else if((i==1)) {
			name_counter = "запись";
		} else {
			name_counter = "записи";
		}
		var htmlconfirm = '<form action="" method="post" id="form_delete_submit" class="ui-state-error ui-corner-all"><p><span class="ui-icon ui-icon-trash" style="float:left;margin-right:0.3em;"></span>'+delhtmlhidden+'удалить <strong>'+i+'</strong> '+name_counter+'? &nbsp; <a href="javascript:void(0);" class="delete_form_return">Удалить</a> <a href="javascript:void(0);" onclick="javascript:sbrosChecked(\'#form_delete_submit\');">Отмена</a></p></form>';			
		$("#confirm").html(htmlconfirm);			
	}
	else {
		$("#confirm").empty();	
	}
};

powerChecked.defaults = {
	nameInput: "del[]",
	typeClick: "all"
};

/* Сброс чекбоксов */
var sbrosChecked = function(message_form, options){
	var settings = jQuery.extend({}, sbrosChecked.defaults, options);
	$("input[name='"+settings.nameInput+"']").each(function(){
		if($(this).is(':checked')) {
			$(this).attr('checked', false);
		}
	});
	$(message_form).fadeAndRemove();
};

sbrosChecked.defaults = {
	nameInput: "del[]"
};

/* Table */
var table_color = function (class_table, options) {
	var settings = jQuery.extend({}, table_color.defaults, options);
	$("."+class_table+" tbody tr:even").addClass(settings.classEven);
	$("."+class_table+" tbody tr").hover(function(){$(this).addClass(settings.classOver)}, function(){$(this).removeClass(settings.classOver)});
};
table_color.defaults = {
	classEven: "darktd",
	classOver: "over"
};	